package com.xzzz.backend.blog.server.base.auth;

import com.xzzz.backend.blog.server.base.user.UserService;
import com.xzzz.backend.blog.server.base.user.pojo.UserEntity;
import com.xzzz.irda.auth.server.service.AbstractUserDetailServiceAdapter;
import com.xzzz.irda.auth.server.service.UserDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class UserDetailServiceImpl extends AbstractUserDetailServiceAdapter {

    @Autowired
    private UserService userService;

    @Override
    public UserDetail loadUserByUsername(String username) {
        UserEntity user = userService.selectByUsername(username);
        AuthUserDetail auth = new AuthUserDetail();

        auth.setUsername(username);
        auth.setUserId(user.getId());
        auth.setPassword(user.getPassword());
        auth.setSalt(user.getSalt());

        Map<String, String> metadata = new HashMap<>();
        metadata.put("username", username);
        auth.setMetadata(metadata);
        return auth;
    }
}
